package learning.homework1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Server {
	public static void main(String[] args) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
			ServerSocket ss = new ServerSocket(2222);
			System.out.println("waiting.....");
			Socket s = ss.accept();
			System.out.println("wellcome to join us");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					s.getInputStream()));
			BufferedReader brs = new BufferedReader(new InputStreamReader(
					System.in));
			BufferedWriter bw_s = new BufferedWriter(new OutputStreamWriter(
					s.getOutputStream()));
			while (true) {
				p(br.readLine());
				p("please enter your msg(Server):");
				String str = brs.readLine();
				bw_s.write("Server says:  " + str + "  " + sdf.format(new Date()));
				bw_s.newLine();
				bw_s.flush();
				if (str.equals("over"))
					break;
			}
			br.close();
			s.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static void p(Object o) {
		System.out.println(o);
	}
}
